
import 'dart:convert';

import '../../../data/models/auth/user.dart';
import '../../exercises/exercises_page/models/exercise.dart';
import '../../gyms/gyms_page/models/gym.dart';

Attendance attendanceFromJson(String str) => Attendance.fromJson(json.decode(str));

String attendanceToJson(Attendance data) => json.encode(data.toJson());

class Attendance {
  final String attendanceId;
  final String userId;
  final String exerciseId;
  final DateTime datetime;
  final String? createdBy;
  final DateTime? createAt;
  final DateTime? updateAt;
  final User? user;
  final Exercise? exercise;
  final Gym? gym;

  Attendance({
    required this.attendanceId,
    required this.userId,
    required this.exerciseId,
    required this.datetime,
    this.createdBy,
    this.createAt,
    this.updateAt,
    this.user,
    this.exercise,
    this.gym,
  });

  Attendance copyWith({
    String? attendanceId,
    String? userId,
    String? exerciseId,
    DateTime? datetime,
    String? createdBy,
    DateTime? createAt,
    DateTime? updateAt,
    User? user,
    Exercise? exercise,
    Gym? gym,
  }) =>
      Attendance(
        attendanceId: attendanceId ?? this.attendanceId,
        userId: userId ?? this.userId,
        exerciseId: exerciseId ?? this.exerciseId,
        datetime: datetime ?? this.datetime,
        createdBy: createdBy ?? this.createdBy,
        createAt: createAt ?? this.createAt,
        updateAt: updateAt ?? this.updateAt,
        user: user ?? this.user,
        exercise: exercise ?? this.exercise,
        gym: gym ?? this.gym,
      );

  factory Attendance.fromJson(Map<String, dynamic> json) => Attendance(
    attendanceId: json["attendance_id"].toString(),
    userId: json["user_id"].toString(),
    exerciseId: json["exercise_id"].toString(),
    datetime: DateTime.parse(json["datetime"]),
    createdBy: json["created_by"]?.toString(),
    createAt: DateTime.parse(json["create_at"]),
    updateAt: DateTime.parse(json["update_at"]),
    user:json["user"]!= null? User.fromJson(json["user"]):null,
    exercise:json["exercise"]!= null? Exercise.fromJson(json["exercise"]):null,
    gym:json["gym"]!= null? Gym.fromJson(json["gym"]):null,
  );

  Map<String, dynamic> toJson() => {
    "attendance_id": attendanceId,
    "user_id": userId,
    "exercise_id": exerciseId,
    "datetime": datetime.toIso8601String(),
    "created_by": createdBy,
    "create_at": createAt?.toIso8601String(),
    "update_at": updateAt?.toIso8601String(),
    "user": user?.toJson(),
    "exercise": exercise?.toJson(),
    "gym": gym?.toJson(),
  };
}

